#include <cctype>
#include <vector>
#include <algorithm>
#include <string>

#include "helpers.h"

using namespace std;

vector<string> SplitByDelimiter(string str, string delimiters) {
  vector<string> res;
  int last = 0;
  int pos = str.find_first_of(delimiters);
  while (pos != (int)string::npos) {
    string pre = str.substr(last, pos-last);
    if (pre.size())
      res.push_back(pre);
    res.push_back(str.substr(pos, 1));
    last = pos+1;
    str[pos] = 1; // We assume character ascii 1 does not appear in delimiters.
    pos = str.find_first_of(delimiters);
  }
  if (last != (int)str.size())
    res.push_back(str.substr(last, (int)str.size()-last));
  return res; 
}

string Strim(string &s) {
  string line = s;
  line.erase(line.begin(), find_if(line.begin(),
                                   line.end(),
                                   not1(ptr_fun<int, int>(isspace))));
  line.erase(find_if(line.rbegin(),
                     line.rend(),
                     not1(ptr_fun<int, int>(isspace))).base(), line.end());
  return line;
}

bool IsANumber(string s) {
  s = Strim(s);
  int points = 0;
  for (int i = 0; i < (int)s.size(); ++i)
    if (s[i] == '.') points++;
    else if (s[i] < '0' or s[i] > '9') return false;
  if (s.size() == 0) return false;
  if (s.size() == 1 && points) return false;
  return points <= 1;
}

bool IsAVariable(string s) {
  s = Strim(s);
  if (!isalpha(s[0])) return false;
  for (int i = 0; i < (int)s.size(); ++i)
    if (!isalnum(s[i])) return false;
  return true;
}
